<template>
  <div>
    <span>员工管理</span>
    <page-tools>
      <template #left>
        <!-- 左侧 -->
        <div class="tips">
          <i class="el-icon-info" />
          <span>本月: 社保在缴 公积金在缴</span>
        </div>
      </template>
      <template #right>
        <!-- 右侧 -->
        <el-button type="primary" size="small">历史归档</el-button>
        <el-button type="primary" size="small" @click="exportexcel">导出</el-button>
        <el-button type="primary" size="small" @click="$router.push('/import')">导入</el-button>
        <el-button type="primary" size="small" @click="closeDialog">新增员工</el-button>

      </template>
    </page-tools>

    <el-card style="margin-top: 10px;">
      <el-table border :data="list">
        <el-table-column width="60" label="序号" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="工号" prop="workNumber" />

        <el-table-column label="聘用形式">
          <template #default="{ row }">
            {{ formEmployment(row.formOfEmployment) }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template #default="{ row }">
            {{ row.timeOfEntry | timeOfEntry }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="{row}">
            <el-button type="text" size="small" @click="openDetail(row.id)">查看</el-button>
            <el-button type="text" size="small" @click="ass(row.id)">分配角色</el-button>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <AddEmpioyee :visible="visible" @openDialog="openDialog" />
    <el-pagination
      stype="flex"
      justify="center"
      align="middle"
      :page-sizes="[5, 10, 15, 20, 25]"
      :page-size="query.size"
      :current-page="query.page"
      layout="total,  prev, pager, next,sizes, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      class="assign-role"
      title="分配角色"
      :visible="showRoleDialog"
      @close="openDialog"
      @open="dialog"
    >
      <!-- 这里准备复选框 -->
      <template>
        <el-checkbox-group v-model="checkList">
          <el-checkbox
            v-for="item in roleList"
            :key="item.id"
            :label="item.id"
          >
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </template>

      <template #footer>
        <el-button type="primary" size="small" @click="confirmAssgin">确定</el-button>
        <el-button size="small" @click="closeAssignDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import dayjs from 'dayjs'
import { empioyeeAPI } from '@/api/empioyee'
import AddEmpioyee from './components/add-empioyee.vue'
import { getExportData } from '@/utils/exce'
import { getUserDetailById, getempioyeeAPI } from '@/api/user'
import { settingAPI } from '@/api/Setting'
export default {
  components: {
    AddEmpioyee
  },
  filters: {
    timeOfEntry(val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      list: [],
      query: {
        page: 1,
        size: 10
      },
      total: 0,
      visible: false,
      showRoleDialog: false,
      checkList: [],
      roleList: [],
      cuID: null
    }
  },
  mounted() {
    this.listempioyee()
  },
  methods: {
    handleSizeChange(val) {
      this.query.size = val
      console.log(val)
      this.listempioyee()
    },
    handleCurrentChange(val) {
      this.query.page = val
      console.log(val)
      this.listempioyee()
    },
    async listempioyee() {
      const res = await empioyeeAPI(this.query)
      this.list = res.rows
      this.total = res.total
    },
    formEmployment(value) {
      const map = {
        1: '正式',
        2: '非正式',
        4: '其他'
      }
      return map[value]
    },
    closeDialog() {
      this.visible = true
    },

    openDialog() {
      this.visible = false
      this.showRoleDialog = false
    },
    exportexcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        // const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
        // const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
        // const data = []
        const res = await empioyeeAPI(this.query)
        const headerRelation = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        const { data } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header: Object.keys(headerRelation), // 配置表头位置  数组格式
          data, // 配置标题位置   二维数组
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 单元格宽度是否撑开
          bookType: 'xlsx'// excle文件类型
        })
      })
    },
    openDetail(id) {
      this.$router.push({
        path: '/detail',
        query: { id }
      })
    },
    ass(id) {
      this.cuID = id
      this.showRoleDialog = true
    },
    closeAssignDialog() {
      this.showRoleDialog = false
    },
    async dialog() {
      // page pagesize
      // 1. 准备一个分页器 2.角色一般的数量不足以进行分页 [page:1,pagesize:100]
      const res = await settingAPI({
        page: 1,
        pagesize: 100
      })
      this.roleList = res.rows
      // 获取一下详情
      const detail = await getUserDetailById(this.cuID)
      console.log(detail)
      this.checkList = detail.roleIds
    },
    async confirmAssgin() {
      await getempioyeeAPI({
        id: this.cuID,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功')
      this.showRoleDialog = false
    }
  }
}
</script>
